home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 1.iso / toolbox / src / exampleCode / opengl / cap / x_interfere.c < prev   
C/C++ Source or Header  |  1996-11-11  |  7KB  |  238 lines

  1. /*
  2.  * Copyright (C) 1994, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17. #include <sys/types.h>
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20.  
  21. #include <X11/Intrinsic.h>
  22. #include <X11/StringDefs.h>
  23. #include <Xm/Xm.h>
  24. #include <Xm/Form.h>
  25. #include <Xm/Frame.h>
  26. #include <Xm/PushB.h>
  27.  
  28. #include "interfere.h"
  29.  
  30. /*
  31.  * For naming the widgets' resources.
  32.  */
  33. #define APP_NAME "interfere"
  34. #define APP_CLASS "Interfere"
  35.  
  36. /*
  37.  * In case there's no app-defaults file.
  38.  */
  39. static String fallbacks[] =
  40. {
  41.     "*canvasFrame.width: 500",
  42.     "*canvasFrame.height: 400",
  43.     "*canvasFrame.leftOffset: 20",
  44.     "*canvasFrame.topOffset: 40",
  45.     "*canvasFrame.rightOffset: 120",
  46.     "*canvasFrame.bottomOffset: 40",
  47.     "*Quit.rightOffset: 10",
  48.     "*Quit.bottomOffset: 10",
  49.     "*Animate.rightOffset: 10",
  50.     "*Animate.bottomOffset: 50",
  51.     "*Capping.rightOffset: 10",
  52.     "*Capping.bottomOffset: 90",
  53.     NULL,
  54. };
  55.  
  56. enum {
  57.     NO_CAPPING = 0, INTERFERENCE_CHECKING
  58. };
  59.  
  60. Boolean do_cap = False;
  61.  
  62. static void
  63. interferenceCallback (Widget w, XtPointer client, XtPointer callback)
  64. {
  65.     static unsigned int state = 0;
  66.     XmString string;
  67.  
  68.     if (state == NO_CAPPING) {
  69.         state = INTERFERENCE_CHECKING;
  70.         string = XmStringCreateSimple("No Capping");
  71.         do_cap = True;
  72.     }
  73.     else {
  74.         state = NO_CAPPING;
  75.         string = XmStringCreateSimple("Capping");
  76.         do_cap = False;
  77.     }
  78.  
  79.     XtVaSetValues(w, XmNlabelString, string, NULL);
  80.     XmStringFree(string);
  81.  
  82.     drawScene ();
  83. }
  84.  
  85. /*
  86.  * This is called until we press the 'Stop' button.
  87.  */
  88. static Boolean 
  89. animateWorkProc (XtPointer client_data)
  90. {
  91.     drawScene ();
  92.     return False; 
  93. }
  94.  
  95. /*
  96.  * Used when the Animate/Stop button is pressed.
  97.  */
  98. static void 
  99. animateCallback (Widget w, XtPointer client, XtPointer callback)
  100. {
  101.     static Boolean toggle = False;
  102.     static work_proc_id = 0;
  103.     XmString string;
  104.     XtAppContext app_context = (XtAppContext)client;
  105.  
  106.     if (!toggle) {
  107.         Boolean animateWorkProc(XtPointer);
  108.         work_proc_id = XtAppAddWorkProc(app_context, animateWorkProc, NULL);
  109.         string = XmStringCreateSimple("Stop");
  110.         toggle = True;
  111.     }
  112.     else {
  113.         XtRemoveWorkProc(work_proc_id);
  114.         string = XmStringCreateSimple("Animate");
  115.         toggle = False;
  116.     }
  117.  
  118.     XtVaSetValues(w, XmNlabelString, string, NULL);
  119.     XmStringFree(string);
  120. }
  121.  
  122. /*
  123.  *
  124.  */
  125. static void 
  126. quitCallback (Widget w, XtPointer client, XtPointer callback)
  127. {
  128.     exit(0);
  129. }
  130.  
  131. /*
  132.  * Set up the application's desktop.
  133.  */
  134. Widget 
  135. setupDesktop (Widget parent, XtAppContext app_context)
  136. {
  137.     Widget quit_button = XtVaCreateManagedWidget
  138.                           ("Quit", xmPushButtonWidgetClass, parent,
  139.                            XmNrightAttachment, XmATTACH_FORM,
  140.                            XmNbottomAttachment, XmATTACH_FORM,
  141.                            XmNtraversalOn, False,
  142.                            NULL);
  143.     Widget animate_button = XtVaCreateManagedWidget
  144.                              ("Animate", xmPushButtonWidgetClass, parent,
  145.                               XmNrightAttachment, XmATTACH_FORM,
  146.                               XmNbottomAttachment, XmATTACH_FORM,
  147.                               XmNtraversalOn, False,
  148.                               NULL);
  149.     Widget int_button = XtVaCreateManagedWidget
  150.                          ("Capping", xmPushButtonWidgetClass, parent,
  151.                           XmNrightAttachment, XmATTACH_FORM,
  152.                           XmNbottomAttachment, XmATTACH_FORM,
  153.                           XmNtraversalOn, False,
  154.                           NULL);
  155.  
  156.     Widget frame = XtVaCreateWidget("canvasFrame", xmFrameWidgetClass,
  157.                                     parent,
  158.                                     XmNbottomAttachment, XmATTACH_FORM,
  159.                                     XmNtopAttachment, XmATTACH_FORM,
  160.                                     XmNleftAttachment, XmATTACH_FORM,
  161.                                     XmNrightAttachment, XmATTACH_FORM,
  162.                                     NULL);
  163.  
  164.  
  165.     XtAddCallback(animate_button, XmNactivateCallback, animateCallback, 
  166.                   app_context);
  167.     XtAddCallback(int_button, XmNactivateCallback, interferenceCallback, NULL);
  168.     XtAddCallback(quit_button, XmNactivateCallback, quitCallback, NULL);
  169.     return frame;
  170. }
  171.  
  172. /* -------------------------------------------------------------- */
  173.  
  174.  
  175. static 
  176. Boolean queryServer (Display * display) 
  177. {
  178.     int major_opcode = 0, first_event = 0, first_error = 0;
  179.     return XQueryExtension(display, "SGI-SUNDRY-NONSTANDARD",
  180.                            &major_opcode, &first_event, &first_error);
  181. }
  182.  
  183. int main (int argc, char ** argv)
  184. {
  185.     XtAppContext app_context;
  186.     Widget toplevel = NULL;
  187.     Widget form = NULL, frame = NULL;
  188.     Display * display = NULL;
  189.     XVisualInfo * visual_info = NULL;
  190.     Colormap cmap;
  191.     int num_options = 0;
  192.     Arg args[0x20];
  193.     int num_args = 0;
  194.  
  195.     XtToolkitInitialize();
  196.     app_context = XtCreateApplicationContext ();
  197.     XtAppSetFallbackResources(app_context, fallbacks);
  198.     display = XtOpenDisplay(app_context, NULL, APP_NAME, APP_CLASS,
  199.                             NULL, 0, &argc, argv);
  200.  
  201.     XtSetArg(args[num_args], XtNargc, argc); num_args++;
  202.     XtSetArg(args[num_args], XtNargv, argv); num_args++;
  203.     visual_info = getVisualInfo(display);
  204.     if (visual_info != NULL) {
  205.         cmap = XCreateColormap(display, 
  206.                                RootWindow(display, visual_info->screen),
  207.                                visual_info->visual, AllocNone);
  208.         XtSetArg(args[num_args], XtNvisual, visual_info->visual); num_args++;
  209.         XtSetArg(args[num_args], XtNdepth, visual_info->depth); num_args++;
  210.         XtSetArg(args[num_args], XtNcolormap, cmap); num_args++;
  211.     }
  212.     toplevel = XtAppCreateShell(APP_NAME, APP_CLASS,
  213.                                 applicationShellWidgetClass,
  214.                                 display, 
  215.                                 args, num_args);
  216.  
  217.     if (queryServer(XtDisplay(toplevel)) == True) {
  218.         form = XtVaCreateWidget("form", xmFormWidgetClass, toplevel,
  219.                                 NULL);
  220.     
  221.             /* Setup the desktop and return the widge that will be the 
  222.                parent to the rendering area. The frame is unmanaged.
  223.                We take care of this inside initRenderingArea() */
  224.         frame = setupDesktop(form, app_context);
  225.         initRenderingArea(frame);
  226.     
  227.         XtManageChild(form);
  228.         XtRealizeWidget(toplevel);
  229.         setCurrentRenderingContext(display);
  230.         XtAppMainLoop(app_context);
  231.     }
  232.     else {
  233.         fprintf(stderr, "You must run this on an SGI server\n");
  234.         return 1;
  235.     }
  236. }
  237.  
  238.